import 'package:enjoy_plus_hm/utils/eventBus.dart';
import 'package:enjoy_plus_hm/utils/http.dart';
import 'package:flutter/material.dart';

import '../../utils/toast.dart';
import 'components/house_item.dart';

class HouseList extends StatefulWidget {
  const HouseList({super.key});

  @override
  State<HouseList> createState() => _HouseListState();
}

class _HouseListState extends State<HouseList> {
  // 房屋列表
  List houseList = [];
  // 是否加载数据
  bool isLoadData = false;

  //获取房屋列表
  getHouseList() async {
    try {
      isLoadData = true;
      var res = await MyHttp.get('/room');
      if (res['code'] != 10000) return ToastUtil.showError('获取数据失败');
      ToastUtil.showSuccess('获取数据成功');
      setState(() {
        houseList = res['data'];
      });
    } catch (e) {
      debugPrint('请求失败：$e');
      ToastUtil.showError('获取数据失败');
    } finally {
      isLoadData = false;
    }
  }

  @override
  void initState() {
    super.initState();
    getHouseList();
    // 监听刷新事件
    eventBus.on<RefreshHouseList>().listen((event) {
      setState(() {
        houseList = [];
        getHouseList();
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color.fromARGB(255, 241, 238, 238),
        appBar: AppBar(
          title: const Text('我的房屋'),
          backgroundColor: Colors.transparent,
          centerTitle: true,
        ),
        body: isLoadData
            ? const Center(
                child: CircularProgressIndicator(),
              )
            : Stack(children: [
                Padding(
                  padding:
                      const EdgeInsets.only(bottom: 80, left: 10, right: 10),
                  child: ListView.separated(
                      itemBuilder: (BuildContext context, int index) {
                        return HouseItem(
                          // 房屋列表项
                          houseInfo: houseList[index],
                        );
                      },
                      separatorBuilder: (BuildContext context, int index) {
                        return Container(
                          height: 10,
                          color: const Color.fromARGB(255, 241, 238, 238),
                        );
                      },
                      itemCount: houseList.length),
                ),
                Positioned(
                    bottom: 0,
                    left: 0,
                    right: 0,
                    child: Container(
                        padding: const EdgeInsets.all(10),
                        color: Colors.transparent,
                        child: Row(children: [
                          Expanded(
                              child: ElevatedButton(
                                  onPressed: () {
                                    Navigator.pushNamed(
                                        context, '/location_list');
                                  },
                                  child: const Column(children: [
                                    SizedBox(height: 5),
                                    Icon(Icons.add),
                                    Text('添加房屋'),
                                    SizedBox(height: 5),
                                  ])))
                        ])))
              ]));
  }
}
